/* 
 * File:   CWorld.cpp
 * Author: ja1e09
 * 
 * Created on August 14, 2011, 3:32 PM
 */

#include "CWorld.h"
#include "Config.h"

CWorld* CWorld::pInstance = 0;

CWorld::CWorld(void)
{
    
}

CWorld* CWorld::GetInstance(void)
{
    if (pInstance == 0)
        pInstance = new CWorld;

    return pInstance;
}

void CWorld::Destroy(void)
{
    if (pInstance != 0)
    {
        SAFE_DELETE(pInstance);
    }
}

void CWorld::Run()
{
    std::ostringstream ssMsg;
    
  // Get Simulation Parameters from the main Configuration Singleton
  uNumRuns=Config::GetInstance()->GetUInt("uNumRuns");  
  uSimTime=Config::GetInstance()->GetUInt("uSimTime");  
  uSimTSs=Config::GetInstance()->GetUInt("uSimTSs");  
  iNumAgents=Config::GetInstance()->GetInt("iNumAgents");
  
  // Compute the total number of time steps in every simulation run
  lTotalSimTSs=uSimTime*uSimTSs;
  
  // Runs Loop
  for(register unsigned int uRunCnt=0;uRunCnt<uNumRuns;uRunCnt++)
  {
      // Init Run
      dSimCurrentTime=0.0;
      
      // Init Population
      
      // Init World
      
      BulletWrapper::GetInstance()->InitPhysics();
      
      // Time steps loop
      for(register unsigned long uTSCnt=0;uTSCnt<lTotalSimTSs;uTSCnt++)
      {
        // Send Agent State to the Agents
        // MPI Broadcast blah blah ...
          
        // Wait for intentions of every agent
        // MPI Receive blah blah blah ...
          
        // Compute new World state
          
        // Compute new Agents state
        
        // Update Simulation Time 
        dSimCurrentTime=(double)uTSCnt/uSimTSs;
        
        // Add TS Data to Simulation Log
        
        // Console Info
        ssMsg << "CWorld::Run::Simulation Running. Step: " << uTSCnt << " Time: " << dSimCurrentTime << " sseconds.";
        Console::GetInstance()->Add(ssMsg.str());
      }
      
      // End Run
      BulletWrapper::GetInstance()->ReleasePhysics();
      
      BulletWrapper::GetInstance()->EndPhysics();
  }
}

